package com.matrix.index.ui.anime.fragment

import android.view.LayoutInflater
import com.blankj.utilcode.util.ThreadUtils.runOnUiThread
import com.matrix.basecore.arouter.ARouterUtils
import com.matrix.basecore.base.BaseFragment
import com.matrix.index.R
import com.matrix.index.databinding.FragmentAnimBinding
import com.matrix.index.ui.anime.adapter.AnimAdapter
import com.matrix.index.ui.anime.adapter.BitmapCache
import com.matrix.index.ui.anime.bean.Root
import com.matrix.index.ui.anime.utils.AssetsUtils
import com.matrix.index.views.hive.HiveLayoutManager
import kotlin.concurrent.thread

/**
 * 动漫fragment
 */
class AnimFragment : BaseFragment<FragmentAnimBinding>() {
    private lateinit var adapter: AnimAdapter
    private var size = 0
    private var roots: ArrayList<Root> = arrayListOf()
    override fun getViewBinding(): FragmentAnimBinding {
        return FragmentAnimBinding.inflate(layoutInflater)
    }

    override fun initView(inflater: LayoutInflater) {
        adapter = AnimAdapter(R.layout.anim_item_layout, roots)
        var view = LayoutInflater.from(context).inflate(R.layout.header_anim_layout, null, false);
        var layoutManager = HiveLayoutManager(HiveLayoutManager.VERTICAL)
        mViewBinding.recyclerview.layoutManager = layoutManager
        mViewBinding.recyclerview.adapter = adapter
    }

    override fun initData() {
        BitmapCache.INSTANCE.init(context, 200 * 200 * 4 * 13);
        roots = AssetsUtils.getAssetsData(requireContext())
        size = roots.size
        var rootNew: ArrayList<Root> = arrayListOf()
        adapter.setList(rootNew)
//        rootNew.addAll(roots)
//        rootNew.addAll(roots)
//        roots.remove(roots[roots.size - 1])
//        roots.remove(roots[roots.size - 1])
        rootNew.addAll(roots)
        thread {
            for (i in 0..<rootNew.size) {
                Thread.sleep(200)
                runOnUiThread {
                    adapter.addData(rootNew[i])
                }
            }
        }
    }

    override fun initListener() {
        adapter.setOnItemClickListener { _, _, position ->
            ARouterUtils.startAnimCategoryActivityPath(position % size)
        }
    }
}